package cn.chinaunicom.sdsi.gacx.projApply.controller;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import cn.chinaunicom.sdsi.frm.base.BaseService;
import cn.chinaunicom.sdsi.frm.sys.R;
import cn.chinaunicom.sdsi.frm.sys.entity.SysStaff;
import cn.chinaunicom.sdsi.gacx.common.FileUtils;
import cn.chinaunicom.sdsi.gacx.projApply.entity.TravelApplyEntity;
import cn.chinaunicom.sdsi.gacx.projApply.entity.TravelApplyFileEntity;
import cn.chinaunicom.sdsi.gacx.projApply.service.projApplyService;
import cn.chinaunicom.sdsi.gacx.workflow.elecmanage.service.ElecBusinessService;
import cn.chinaunicom.sdsi.upfc_demp.demo.controller.UpfDemoTestController;
import cn.chinaunicom.sdsi.upfc_demp.dzgh.entity.DzghApplyEntity;

/**
 * 旅游项目流程管理
 * @author xp
 * 2018-03-12
 */
@Controller
@RequestMapping("/projApplyController")
public class projApplyController{
	private final Log log = LogFactory.getLog(UpfDemoTestController.class);
	
	@Value("${UPLOAD_GAAJ_FILE_PATH}")//公安案件上传地址
    private String path;
	
	@Autowired
	private ElecBusinessService elecBusinessService;
	
	@Autowired
	private projApplyService projApplyService;
	
	
	@RequestMapping("/showProjApply")
	public ModelAndView showProjApply(){
		ModelAndView mv = new ModelAndView();
		// 获取电子公函编码
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");// 设置日期格式
		String proj_id = "XMBH" + df.format(new Date()) + ((int) (Math.random() * 900) + 100);// new
		
		mv.addObject("projId",proj_id);																								
		mv.setViewName("gacx/workflow/travelmanager/travel_apply");
		return mv;
	}
	
	

	
	/**
	 * 手动删除附件
	 * @param projId
	 * @param oldFileName
	 * @param request
	 * @return
	 */
	@RequestMapping("/delAttachment")
    @ResponseBody
	public boolean delAttachment(String projId,String oldFileName, HttpServletRequest request){
		log.info("删除旅游项目附件！");
		int i = projApplyService.deleteTravelFileByHand(projId, oldFileName);
		if(i >= 0){
			return true;
		}else{
			return false;
		}
	}
	
	 /**
     * 上传附件
     * @author xp
     * 2018-03-14
     */
    @RequestMapping("/uploadAttachment")
    @ResponseBody
    public JSONObject uploadAttachment(TravelApplyEntity travelApplyEntity) throws Exception {
        log.info("电子公函申请上传案件附件");
        
        MultipartFile myfile = travelApplyEntity.getMyfile();
        long length = myfile.getSize();
        
        String projId = travelApplyEntity.getProjId();
        
        int pre = (int) System.currentTimeMillis();
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd HH:mm:ss:SSS");
        String oldFileName = "";//原文件名
        String newFileName = "";//新文件名
        boolean flag = false;
        if (myfile != null) {
            //取得当前上传文件的文件名称  
        	oldFileName = myfile.getOriginalFilename();
            String end = FileUtils.getExtend(oldFileName);//获取文件的扩展名
            //如果名称不为“”,说明该文件存在，否则说明该文件不存在  
            if (oldFileName.trim() != "") {
                System.out.println(oldFileName);
                String in_date = df.format(new Date()).replaceAll(" ", "").replaceAll(":", "");//获取当前时间
                //重命名上传后的文件名  
                newFileName = "Travel" + in_date + "." + end;  //重命名格式：GaAj+当前时间+随机数
                //定义上传路径  
                //String path = "D:/upload/";
                String uploadpath = "";
                if (path.indexOf(".") != -1) {
                    uploadpath = path;
                } else {
                    uploadpath = path + newFileName;
                }
                try {
                    File localFile = new File(uploadpath);
                    myfile.transferTo(localFile);
                    flag = true;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        //记录上传该文件后的时间  
        int finaltime = (int) System.currentTimeMillis();
        System.out.println(finaltime - pre);

        JSONObject obj = new JSONObject();
        if (flag) {
            //先删除
            projApplyService.deleteTravelFile(projId);
            //再添加
            TravelApplyFileEntity travelApplyFileEntity =new TravelApplyFileEntity();
            travelApplyFileEntity.setProjId(projId);
            travelApplyFileEntity.setNewFileName(newFileName);
            travelApplyFileEntity.setOldFileName(oldFileName);
            
            projApplyService.addTravelFile(travelApplyFileEntity);
            obj.put("msg", "附件上传成功!");
        } else {
            obj.put("msg", "文件为空");
        }
        obj.put("oldname", oldFileName);
        obj.put("path", false);
        obj.put("size", (length / 1024));
        obj.put("length", length);

        return obj;
    }
    
  
}
